<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

defined('IN_ADMIN') or die('Hacking Attempt!');

func::isLogin();
mysql::begin(1);

$sTable = 'payment';
$iTotal = 0;

$iCustomerId = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : 0;

if(isset($_POST['addPaymentButton'])){
    $aVals = array(
        'customerId' => isset($_POST['customerId']) ? $_POST['customerId'] : 0,
        'orderId' => isset($_POST['orderId']) ? $_POST['orderId'] : 0,
        'amount' => (isset($_POST['amount']) && is_numeric(func::numberFormatInput($_POST['amount']))) ? func::numberFormatInput($_POST['amount']) : 0,
        'date' => time(),
        'username' => $_SESSION[SESSION_NAME]
    );
    $iIdInsertPayment = mysql::insertSql($sTable, $aVals);

    //update debit amount
    $aRow = mysql::selectDebitAmount('debit', $aVals['customerId']);
    $iTotal = $aRow['amount'] - $aVals['amount'];
    $iIdUpdateDebit = mysql::updateSql('debit', array('amount' => $iTotal), 'customerId', $aVals['customerId']);

    if($iIdInsertPayment && $iIdUpdateDebit){
        mysql::commit();
        func::redirect (func::makeUrl ('payment', 'list'));
    }else
        mysql::rollBack();
}else{
    //customer
    $sSql = 'SELECT customerId, name FROM customer ORDER BY name ASC';
    $aResult = mysql::querySql($sSql);
}
?>
